const mysql = require('mysql');
let client = undefined;

// 查询数据命令执行
const query = function (sql, cb) {
    console.log('query = ' + sql);
    // 数据库查询
    client.getConnection(function (err, connection) {
        if (err) {
            console.log('connection mysql err = ' + err);
            cb(err);
            throw err;
        } else {
            connection.query(sql, function (connerr, result, fileds) {
                if (connerr) {
                    console.log('query err = ' + connerr);
                    cb(connerr);
                } else {
                    cb(null, result);
                }
                connection.release();
            });
        }
    }) ;
};

// 执行添加新数据语句合并
const insertSql = function (table, data) {
    let sql = 'insert into ' + table;
    // 语句合并
    let keyStr = ' (';
    let valuesStr = ' values (';
    for (let i in data){
        keyStr += i + ',';
        if ((typeof data[i]).indexOf('string') === 0) {
            valuesStr += "'" + data[i] + "'" + ',';
        } else {
            valuesStr += data[i] + ',';
        }
    }
    keyStr = keyStr.substring(0, keyStr.length - 1);
    valuesStr = valuesStr.substring(0, valuesStr.length - 1);
    keyStr += ')';
    valuesStr += ');';
    sql += keyStr;
    sql += valuesStr;
    return sql;
};

// 执行更新语句合并
const updataSql = function(tebel, key, data){
    let sql = 'update ' + tebel + ' set ';
    // 语句合并
    for (let i in data){
        if ((typeof data[i]).indexOf('string') === 0) {
            sql += i + '=' + "'" + data[i] + "'" + ',';
        } else {
            sql += i + '=' + data[i] + ',';
        }
    }
    sql = sql.substring(0, sql.length - 1);

    for (let i in key) {
        if ((typeof data[i]).indexOf('string') === 0) {
            sql += ' where ' + i + '=' + "'" + key[i] + "'" + ';';
        } else {
            sql += ' where ' + i + '=' + key[i] + ';';
        }
    }
    return sql;
};

// 数据库链接
exports.connect = function (config) {
    client = mysql.createPool(config);
};

// 查找玩家数据
exports.checkPlayer = function (uniqueID, cb) {
    let sql = 'select * from t_playerinfo where unique_id = ' + uniqueID + ';';
    query(sql, function (err, data) {
        if (err) {
            console.log('checkPlayer err = ' + err);
        }
        console.log('uniqueID data = ' + JSON.stringify(data));
        cb(err, data);
    });
};

// 加入数据库（数据写入数据库）
//let sql = 'insert into t_playerinfo (unique_id, uid, nick_name, avatar_url, game_id, house_card_count) values(' + data.uniqueID + ',' + data.uid + ',' + data.nickName + ',' + data.avatarUrl + ',' + data.gameID + ',' + data.houseCardCount + ',' +')';
exports.insertPlayerInfo = function (data) {
    let sql = insertSql('t_playerinfo', data);
    query(sql, function (err, datainfo) {
        if (err){
            console.log('insertPlayerInfo err = ' + err);
        } else {
            console.log('insertPlayerInfo = ' + JSON.stringify(datainfo));
        }
    });
};

// 更新玩家数据
//set sql_safe_updates=0
//update t_playerinfo set nick_name='小',avatar_url='mingrizhixing.com' where unique_id='100000350155';
exports.updataPlayerInfo = function (key, data) {
    let sql = updataSql('t_playerinfo', key, data);
    query(sql, function (err, datainfo) {
        if (err){
            console.log('updataPlayerInfo err = ' + err);
        } else {
            console.log('updataPlayerInfo = ' + JSON.stringify(datainfo));
        }
    });
};